﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SD.IBUS;
using SD.Entities;
using SD.IDAL;
using SD.DAL;

namespace SD.BUS
{
    public class ContactBIZ : BaseBIZ, IContactBIZ
    {
        #region IContactBIZ Members

        /// <summary>
        /// Get existing contact
        /// </summary>
        /// <returns>List of address</returns>
        public DataTable ListContact(Boolean includeInactive)
        {
            DataTable dtbResult = new DataTable();
            dtbResult.Columns.Add("ContactId");
            dtbResult.Columns.Add("ContactName");
            dtbResult.Columns.Add("MobilePhone");
            dtbResult.Columns.Add("Email");
            dtbResult.Columns.Add("ContactType");
            dtbResult.Columns.Add("IsActive");

            try
            {
                //Get list contact
                List<Contact> lstContact = GetAll(includeInactive);
                //If rows of list >0
                if (lstContact.Count > 0)
                {
                    //Get value of contact in list contact
                    foreach (Contact contact in lstContact)
                    {
                        dtbResult.Rows.Add(contact.ContactId, contact.FirstName, contact.MobilePhone, contact.Email,
                                            contact.ContactType != null ? contact.ContactType.RefValue : "N/A",
                                            contact.IsActive);
                    }
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return dtbResult;
        }

        /// <summary>
        /// Get all existing contact
        /// </summary>
        /// <returns>List of contact</returns>
        public List<Contact> GetAll(Boolean includeInactive)
        {
            List<Contact> lstResult = new List<Contact>();

            try
            {
                IContactDAO contactDAO = new ContactDAO();
                lstResult = contactDAO.GetAll(includeInactive);
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return lstResult;
        }

        /// <summary>
        /// process exception for GetById
        /// </summary>
        /// <param name="contactId"></param>
        /// <returns> NotImplementedException </returns>
        public SD.Entities.Contact GetById(string contactId)
        {
            Contact contact = null;
            try
            {
                IContactDAO contactDAO = new ContactDAO();
                contact = contactDAO.GetById(contactId);
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return contact;
        }

        /// <summary>
        /// Insert contact
        /// </summary>
        /// <param name="contact"></param>
        /// <returns>true or false</returns>
        public bool Insert(SD.Entities.Contact contact)
        {
            return false;
        }

        /// <summary>
        /// Process exception for event update
        /// </summary>
        /// <param name="contact"></param>
        /// <returns>NotImplementedException</returns>
        public bool Update(SD.Entities.Contact contact)
        {
            return false;
        }

        /// <summary>
        /// Process exception for delete 
        /// </summary>
        /// <param name="contactId"></param>
        /// <returns>NotImplementedException</returns>
        public bool Delete(string contactId)
        {
            return false;
        }

        /// <summary>
        /// Get all list contact
        /// </summary>
        /// <returns>list contact</returns>
        public List<Contact> GetAllContact()
        {
            List<Contact> lstResult = new List<Contact>();

            try
            {
                IContactDAO contactDAO = new ContactDAO();
                lstResult = contactDAO.GetAllContact();
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return lstResult;
        }

        /// <summary>
        /// Get contact is active
        /// </summary>
        /// <returns>list contact</returns>
        public List<Contact> GetActiveContact()
        {
            List<Contact> lstResult = new List<Contact>();

            try
            {
                IContactDAO contactDAO = new ContactDAO();
                lstResult = contactDAO.GetActiveContact();
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return lstResult;
        }

        /// <summary>
        /// Get list contact by conditions
        /// </summary>
        /// <param name="firstname"></param>
        /// <param name="surname"></param>
        /// <returns>list contact</returns>
        public List<Contact> GetContactByCodition(string firstname, string surname)
        {
            List<Contact> lstResult = new List<Contact>();

            try
            {
                IContactDAO contactDAO = new ContactDAO();
                lstResult = contactDAO.GetContactByCodition(firstname, surname);
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return lstResult;
        }

        /// <summary>
        /// Get data table contact
        /// </summary>
        /// <returns>Data table contact</returns>
        public DataTable ListAllContact()
        {
            DataTable dtbResult = new DataTable();
            dtbResult.Columns.Add("ContactId");
            dtbResult.Columns.Add("ContactName");
            dtbResult.Columns.Add("MobilePhone");
            dtbResult.Columns.Add("Email");
            dtbResult.Columns.Add("ContactType");
            dtbResult.Columns.Add("IsActive");

            try
            {
                List<Contact> lstContact = GetAllContact();
                //If number rows  of list contact > 0
                if (lstContact.Count > 0)
                {
                    //Get contact from list contact
                    foreach (Contact contact in lstContact)
                    {
                        dtbResult.Rows.Add(contact.ContactId, contact.FirstName, contact.MobilePhone, contact.Email,
                                            contact.ContactType != null ? contact.ContactType.RefValue : "N/A",
                                            contact.IsActive);
                    }
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return dtbResult;
        }

        /// <summary>
        /// Get data table contact is active
        /// </summary>
        /// <returns>data table contact</returns>
        public DataTable ListActiveContact()
        {
            DataTable dtbResult = new DataTable();
            dtbResult.Columns.Add("ContactId");
            dtbResult.Columns.Add("ContactName");
            dtbResult.Columns.Add("MobilePhone");
            dtbResult.Columns.Add("Email");
            dtbResult.Columns.Add("ContactType");
            dtbResult.Columns.Add("IsActive");

            try
            {
                List<Contact> lstContact = GetActiveContact();
                //If number rows of list >0
                if (lstContact.Count > 0)
                {
                    //Get contact from list contact
                    foreach (Contact contact in lstContact)
                    {
                        dtbResult.Rows.Add(contact.ContactId, contact.FirstName, contact.MobilePhone, contact.Email,
                                            contact.ContactType != null ? contact.ContactType.RefValue : "N/A",
                                            contact.IsActive);
                    }
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return dtbResult;
        }

        /// <summary>
        /// Get data table contact by condition
        /// </summary>
        /// <param name="firstname"></param>
        /// <param name="surname"></param>
        /// <returns>data table contact</returns>
        public DataTable ListContactByCodition(string firstname, string surname)
        {
            DataTable dtbResult = new DataTable();
            dtbResult.Columns.Add("ContactId");
            dtbResult.Columns.Add("ContactName");
            dtbResult.Columns.Add("MobilePhone");
            dtbResult.Columns.Add("Email");
            dtbResult.Columns.Add("ContactType");
            dtbResult.Columns.Add("IsActive");

            try
            {
                List<Contact> lstContact = GetContactByCodition(firstname, surname);
                //If number rows of list > 0
                if (lstContact.Count > 0)
                {
                    //Get contact from list contact
                    foreach (Contact contact in lstContact)
                    {
                        dtbResult.Rows.Add(contact.ContactId, contact.FirstName, contact.MobilePhone, contact.Email,
                                            contact.ContactType != null ? contact.ContactType.RefValue : "N/A",
                                            contact.IsActive);
                    }
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return dtbResult;
        }

        #endregion
    }
}
